<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


$DB = JFX::registry('db');
$CONFIG = JFX::registry('config');

$groups = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}iusers_groups");


if(Antz_IntelliForm::submitted('update-groups')){
    $allowed = post('groupsAllowed');
  
    $DB->delete($CONFIG->dbprefix.'iusers_blocks_to_groups', "block_id = '{$blockDetails['id']}'");
    foreach($groups as $k=>$v){
        if(!in_array($v['keyname'], $allowed)){
            $dbParams = array(
                'group_key' => $v['keyname'],
                'block_id' => $blockDetails['id']
            );
            $DB->insert($CONFIG->dbprefix.'iusers_blocks_to_groups', $dbParams);
        }
    }
    JFX::addSuccess($this->lang('perms_updated'));
}












$denied = array();
$groupsDenied = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}iusers_blocks_to_groups WHERE block_id = '{$blockDetails['id']}'");
if(!is_array($groupsDenied)) $groupsDenied = array();

foreach($groupsDenied as $k=>$v){
    $denied[] = $v['group_key'];
}

$options = array();
$defValue = array();

foreach($groups as $k=>$grp){
    $options[$grp['keyname']] = $grp['title'].' ['.$grp['keyname'].']';
    if(!in_array($grp['keyname'], $denied)) $defValue[] = $grp['keyname'];
}

$formFields = array(
    array(
        'type' => 'select',
        'name' => 'groupsAllowed[]',
        'label' => 'Groups allowed',
        'multiple' => 'multiple',
        'size' => 6,
        'value' => post('groupsAllowed', $defValue),
        'options' => $options
    ),
    array(
        'type' => 'submit',
        'name' => 'submitBtn',
        'value' => 'Update groups',
        'label' => '&nbsp;'
    )
);

echo JFX::makeRapidForm($formFields, 'Update group settings', 'update-groups');


